package com.mg.admin.controller.admin;


import com.mg.admin.bean.admin.OrganizationVO;
import com.mg.admin.bean.admin.Result;
import com.mg.admin.bean.admin.RoleVO;
import com.mg.admin.bean.admin.UserVO;
import com.mg.admin.constants.ResultEnum;
import com.mg.admin.controller.BaseController;
import com.mg.admin.page.PageBean;
import com.mg.admin.page.PageParam;
import com.mg.admin.service.admin.OrganizationService;
import com.mg.admin.service.admin.RoleService;
import com.mg.admin.service.admin.UserService;
import com.xiaoleilu.hutool.crypto.SecureUtil;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@Controller
@RequestMapping("/admin/user")
public class UserController extends BaseController {

    @Autowired
    private UserService userService;

    @Autowired
    private OrganizationService organizationService;

    @Autowired
    private RoleService roleService;


    @GetMapping(LIST)
    public void list(ModelMap modelMap){
        UserVO userVO = currentUser();
        List<OrganizationVO> voList = organizationService.findChildByParentId(userVO.getOrgId());
        modelMap.put("list",voList);
        modelMap.put("orgId",userVO.getOrgId());
    }

    @ResponseBody
    @PostMapping(LIST)
    public Result<PageBean> pageList(UserVO userVO){
        PageParam pageParam = new PageParam(userVO.getStart(),userVO.getLength());
        UserVO currentUser = currentUser();
        Long orgId = userVO.getOrgId();
        if (orgId == null){
            orgId = currentUser.getOrgId();
        }
        PageBean pageBean = userService.findByParentOrgId(orgId,pageParam);
        return new Result<>(pageBean);
    }

    @GetMapping(ADD)
    @RequiresPermissions("user:add")
    public void add(ModelMap modelMap,long orgId){
        OrganizationVO organizationVO = organizationService.findById(orgId);
        List<RoleVO> roleVOList = roleService.findByOrgId(orgId);
        modelMap.put("list",roleVOList);
        modelMap.put("orgId",orgId);
        modelMap.put("orgName",organizationVO.getName());
    }

    @GetMapping(EDIT)
    @RequiresPermissions("user:edit")
    public void edit(ModelMap modelMap,long id){
        UserVO param = new UserVO();
        param.setId(id);
        UserVO userVO = userService.findByExample(param);
        List<RoleVO> roleVOList = roleService.findByOrgId(userVO.getOrgId());
        modelMap.put("list",roleVOList);
        modelMap.put("bean",userVO);
    }


    @ResponseBody
    @PostMapping(SAVE)
    @RequiresPermissions("user:add")
    public Result save(UserVO userVO){
        UserVO vo = userService.findByUserName(userVO.getUsername());
        if (vo != null && !userVO.getUsername().trim().equals(vo.getUsername())){
            return new Result(ResultEnum.USER_HAS_EXIST);
        }
        if (userVO.getId() == null){
            userService.insertOne(userVO);
        }else {
            userService.updateOne(userVO);
        }
        return new Result();

    }

    @ResponseBody
    @GetMapping(DELETE)
    @RequiresPermissions("user:delete")
    public void delete(Long id){
        userService.delete(id);
    }

    @GetMapping("editpwd")
    public void editpwd(ModelMap modelMap){
        UserVO userVO = currentUser();
        modelMap.put("bean",userVO);
    }

    @ResponseBody
    @PostMapping("pwdupdate")
    public void pwdupdate(UserVO userVO){
        userVO.setPassword(SecureUtil.md5(userVO.getPassword()));
        userService.updateByUserName(userVO);
    }


}